package com.service.weChatWifiLogin.controller;

import com.service.config.annotation.ParamsRequestWrapper;
import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.weChatWifiLogin.entity.UserInfo;
import com.service.weChatWifiLogin.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * (UserInfo)表控制层
 *
 * @author makejava
 * @since 2021-06-29 14:42:45
 */
@RestController
@RequestMapping("userInfo")
@Api(tags = "后台管理接口")
public class UserInfoController {
    /**
     * 服务对象
     */
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 通过主键查询单条数据
     *
     * @param customerServiceInfo
     * @return 单条数据
     */
    @PostMapping(value = "auth",produces="application/json")
    @ApiOperation(value = "统一登录")
    @ResponseBody
    public Result customerLogin(@RequestBody UserInfo customerServiceInfo) throws Exception {
        HashMap map = new HashMap();
        customerServiceInfo= userInfoService.queryAllByNameAndPassword(customerServiceInfo);
        String token = UserTokenManager.generateToken(customerServiceInfo.getId());
        redisUtil.set(token,4, JsonXMLUtils.obj2json(customerServiceInfo),2);
        map.put("Token",token);
        return Result.ok().setData(map);
    }
    /**
     * 客服退出登录
     *
     * @param cloudAuthToken
     * @return 单条数据
     */
    @PostMapping(value = "loginOut",produces="application/json")
    @ApiOperation(value = "客服退出登录")
    @ResponseBody
    public Result customerLogin(@RequestHeader(value = "token",required = false) String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            if (redisUtil.exist(cloudAuthToken)) {
                redisUtil.delete(cloudAuthToken);
                return Result.ok();
            }
        }
        return Result.ok();
    }
}
